from core.logger import log
from db.mysql.dao.devices import DevicesDao
from db.mysql.dao.template import TemplateDao
from db.mysql.template import Template
from db.mysql.users import Users
from db.pydantic.request.template import TemplateCreateRequest, BindTemplateRequest


class TemplateService:
    def __init__(self):
        pass

    def get_template(self,current_user: Users):
        """
        获取模板列表
        """
        try:
            template_dao = TemplateDao()
            # 获取这个用户下的所有模板
            templates:list = template_dao.get_all_templates_by_user(current_user.user_id)
            return templates
        except Exception as e:
            log.info("获取模板列表失败", e)
            return None

    def create_template(self,req: TemplateCreateRequest,current_user: Users):
        try:
            template_dao = TemplateDao()
            template_data = req.model_dump()
            template_data["user_id"] = current_user.user_id
            template = template_dao.create_template(template_data)
            return  template
        except Exception as e:
            log.info("创建模板失败", e)
            return None
    def update_template(self,template_id: int, req: TemplateCreateRequest,current_user: Users):
        try:
            template_dao = TemplateDao()
            template_data = req.model_dump()
            template = template_dao.update_template(template_id, template_data)
            return template
        except Exception as e:
            log.info("更新模板失败", e)
            return None

    def delete_template(self,template_id: int,current_user: Users):
        try:
            template_dao = TemplateDao()
            template = template_dao.delete_template(template_id)
            return template
        except Exception as e:
            log.info("删除模板失败", e)
            return None

    def bind_template(self,device_phone:str,req:BindTemplateRequest,current_user: Users):
        try:
            device_dao = DevicesDao()
            template_dao = TemplateDao()
            device = device_dao.get_device_by_phone(device_phone)
            if device:
                if device.user_id != current_user.user_id:
                    return None
                template = template_dao.get_template_by_id(req.template_id)
                if not template:
                    return None
                device = device_dao.update_device_template_id(device,template_id=req.template_id)
                return device
            raise Exception("设备不存在")
        except Exception as e:
            log.info("绑定模板失败", e)
            return None

    def get_device_config(self,device_phone:str,current_user: Users):
        try:
            device_dao = DevicesDao()
            template_dao = TemplateDao()
            device = device_dao.get_device_by_phone(device_phone)
            if device:
                if device.user_id != current_user.user_id:
                    return None
                template:Template = template_dao.get_template_by_id(device.template_id)
                if not template:
                    return None
                return template.configs
            raise Exception("设备不存在")
        except Exception as e:
            log.info("获取设备配置失败", e)
def get_template_service():
    return TemplateService()